<?php
namespace app\houtai\controller\user\redis;
use app\houtai\controller\BaseController;
use org\GoApi\UserLimitCtrl;

class LimitController extends BaseController {
  /**
   *  @brief  : 读用户区间列表
   *  @return : page
   *  @details:
   */
  public function indexAction() {
    $rows   = [];
    $conds  = [];
    $order  = [];
    $search = $this->request->param('search/s', '', 'trim');
    $CHANNELS = $this->GetAdminChannel();
    $total = 0;
    //////////////////////////////////////////////////////////////////////////////////////
    $time = $this->request->param('time/s', '', 'trim,YmdHis2Unix');
    $time = YmdHis2Unix(date('Y-m-d 00:00:00', $time > 0 ? $time : time()));
    $channel = $this->request->param('channel/d', -1);
    if ($channel > 0 && array_key_exists($channel, $CHANNELS)) {
      $conds['b.Channel'][] = ['IN', array_keys($CHANNELS)];
      $conds['b.Channel'][] = ['EQ', $channel];
    } else {
      $conds['b.Channel'] = ['IN', array_keys($CHANNELS)];
    }
    //////////////////////////////////////////////////////////////////////////////////////
    if ($search > 0) { // 查询指定用户
      $eax = UserLimitCtrl::Read($search);
      if (!empty($eax)) {
        $rows[] = $eax;
      }
    } else {
      $eax = UserLimitCtrl::Page($channel, $this->pageCurrent, $this->pageSize);
      $eax = $eax['PageJson'];
      if (!empty($eax['data'])) {
        $rows = $eax['data'];
        $total = $eax['Total'];
      }
    }
    //////////////////////////////////////////////////////////////////////////////////////
    $listRd = [];
    $listDb = [];
    if (!empty($rows)) {
      foreach($rows as $eax) {
         $eax = json_decode($eax,true);
        $listRd[$eax['GameID']] = $eax;
      }
      //////////////////////////////////////////////////////////////////////////////////////
      $TMS = [
        [$time - 0 * 24 * 60 * 60, $time + 1 * 24 * 60 * 60], // day0
        [$time - 1 * 24 * 60 * 60, $time - 0 * 24 * 60 * 60],
        [$time - 2 * 24 * 60 * 60, $time - 1 * 24 * 60 * 60],
        [$time - 3 * 24 * 60 * 60, $time - 2 * 24 * 60 * 60],
        [$time - 4 * 24 * 60 * 60, $time - 3 * 24 * 60 * 60],
        [$time - 5 * 24 * 60 * 60, $time - 4 * 24 * 60 * 60],
        [$time - 6 * 24 * 60 * 60, $time - 5 * 24 * 60 * 60], // day6
        [$time - 6 * 24 * 60 * 60, $time + 1 * 24 * 60 * 60], // day7 合计
      ];
      $arr = [];
      foreach($TMS as $k=>$v) {
        $key = sprintf("( SELECT ISNULL(SUM (e.lScore), 0) FROM CYRecordDB.dbo.RecordGameDataUserRecord e WHERE e.UserID = b.UserID AND e.CreateTime BETWEEN '%s' AND '%s' )",
          YmdHis($v[0]), YmdHis($v[1])
        );
        $arr[$key] = "day{$k}";
      }
      //////////////////////////////////////////////////////////////////////////////////////
      $orderMap = [
        'B' => 'Channel',
        'C' => 'RegisterDate',
        'D' => 'GameID',
        'I' => 'day6',
        'J' => 'day5',
        'K' => 'day4',
        'L' => 'day3',
        'M' => 'day2',
        'N' => 'day1',
        'O' => 'day0',
        'P' => 'day7',
        'Q' => 'days',
      ];
      $this->orderField = array_key_exists($this->orderField, $orderMap) ? $this->orderField : 'P';
      $order[$orderMap[$this->orderField]] = $this->orderDirection;
      //////////////////////////////////////////////////////////////////////////////////////
      $listDb = table('CYBasicDB.dbo.AccountsInfo b', 'rydb.basic')->field(array_merge([
        'b.UserID', 'b.GameID', 'b.NickName', 'b.Sex', 'b.AccountImgUrl', 'b.Channel',
        'b.AccountsType', 'b.Accounts', 'b.Compellation', 'b.PassPortID', 'b.RegisterMobile',
        'b.RegisterDate', 'b.RegisterIP', 'b.LastLogonDate'=>'GameLogonTimes', 'b.LastLogonDate', 'b.LastLogonIP',
        '( SELECT ISNULL(SUM (e.lScore), 0) FROM CYRecordDB.dbo.RecordGameDataUserRecord e WHERE e.UserID = b.UserID )' => 'days',
      ], $arr))
      ->where('b.GameID', 'IN', array_keys($listRd))
      ->where($conds)
      ->order($order)
      ->select();
      //设置GameID为Key
      $arr = [];
      if (!empty($listDb)) {
        foreach($listDb as $eax) {
          $arr[$eax['GameID']] = $eax;
        }
      }
      $listDb = $arr;
    }
    $path = $this->request->baseUrl();
    $pager = new \org\dtcms($rows, $this->pageSize, $this->pageCurrent, $total, false, ['path'=>$path]);
    //////////////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'search'  => $search,
      'channel' => $channel,
      'time'    => $time,
      'CHANNELS'=> $CHANNELS,
      'listRd'  => $listRd,
      'listDb'  => $listDb,
      'SEXARR'  => [0 => '未知', 1 => '男性', 2 => '女性'],
      'pager'   => $pager,
    ]);
  }
  /**
   *  @brief  : 读写redis用户控制参数
   *  @return : page|JSON
   *  @details:
   */
  public function detailAction() {
    $GameID = $this->request->param('GameID/d', 0);
    if ($this->request->isGet()) { // `读取/新增`模板
      $conf = $GameID > 0 ? UserLimitCtrl::Read($GameID) : [];
      return $this->fetch('', ['GameID' => $GameID, 'Conf' => $conf]);
    } else {
      ////////////////////////////////////
      if ($GameID <= 0) {
        return result([], 10, 'GameID不能为负数');
      }
      $LimitMin = $this->request->param('LimitMin/d', 0);
      $LimitMax  = $this->request->param('LimitMax/d', 0);
      ////////////////////////////////////
      $user = table('AccountsInfo', 'rydb.basic')->field(['GameID', 'Channel','NickName'])->where('GameID', $GameID)->find();
      if (empty($user)) {
        return result([], 12, 'GameID不存在');
      }
      ////////////////////////////////////
      $eax = UserLimitCtrl::Save($GameID, $LimitMin, $LimitMax, $user['Channel'],$user['NickName']);
      return result([], $eax);
    }
  }
  /**
   *  @brief  : 删除操作
   *  @return : JSON
   *  @details:
   */
  public function deloneAction() {
    $GameID = $this->request->param('GameID/d', 0);
    if ($GameID <= 0) {
      return result([], 10, 'GameID不能为负数');
    }
    $limit = $GameID > 0 ? UserLimitCtrl::Read($GameID) : [];
    if (empty($limit)) {
      return result([], 11, '区间不存在');
    }
    $eax = UserLimitCtrl::Remove($GameID);
    return result([], $eax);
  }
}
